package cn.wolfcode.p2p.base.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.RealAuthQuery;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.LoginInfoContext;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {
    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
	private IUserInfoService userInfoService;
	
	@Transactional(readOnly=true)
	public PageInfo<RealAuth> query(RealAuthQuery qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getGroupType());
		List<RealAuth> list = realAuthMapper.selectList(qo);
		return new PageInfo<RealAuth>(list);
	}

	@Override
	public void saveRealAuth(RealAuth vo) {
		LoginInfo loginInfo = LoginInfoContext.getCurrentLoginInfo();
		// 设置申请人, 申请时间, 状态为待审核
        LoginInfo applier = new LoginInfo();

        // 创建一个认证信息对象, 将vo的值设置到realAuth
        applier.setId(loginInfo.getId());
        RealAuth realAuth = new RealAuth();
        realAuth.setIdNumber(vo.getIdNumber());
        realAuth.setRealName(vo.getRealName());
        realAuth.setSex(vo.getSex());
        realAuth.setBornDate(vo.getBornDate());
        realAuth.setAddress(vo.getAddress());
        realAuth.setState(vo.getState());
        realAuth.setImage1(vo.getImage1());
        realAuth.setImage2(vo.getImage2());
        realAuth.setApplyTime(new Date());
        realAuth.setApplier(applier);


        realAuth.setState(RealAuth.STATE_WAIT)
                .setApplyTime(new Date())
                .setApplier(applier);

        realAuthMapper.insert(realAuth);

		// 将保存的认证信息, 设置到用户信息的身份信息中
		UserInfo userInfo = userInfoService.get(loginInfo.getId());
		userInfo.setRealAuthId(realAuth.getId());

		// 修改用户信息
		userInfoService.update(userInfo);
	}

    @Override
    public RealAuth get(Long id) {
        return realAuthMapper.selectById(id);
    }

    @Override
    public void auditRealAuth(RealAuth vo) {
	    // 查询身份认证信息
        RealAuth realAuth = this.get(vo.getId());
        // 查询用户信息
        UserInfo userInfo = userInfoService.get(realAuth.getApplier().getId());
        // 判断当前申请人呢是否已经认证成功
        if (!userInfo.getHasRealAuth()){

            // 获取审核人信息
            LoginInfo auditor = (LoginInfoContext.getCurrentLoginInfo());
            // 将审核的详情, 插进身份认证信息
            realAuth.setAuditTime(new Date())
                    .setAuditor(auditor)
                    .setState(vo.getState())
                    .setRemark(vo.getRemark());

            // 更新
            realAuthMapper.updateById(realAuth);

            if (realAuth.getState() == RealAuth.STATE_PASS){
                // 审核通过, 设置状态码
                userInfo.addNewState(BitStateUtil.OP_REAL_AUTH);
                // 设置真实姓名, 身份证号
                userInfo.setRealName(realAuth.getRealName())
                        .setIdNumber(realAuth.getIdNumber());

            }else {
                // 审核拒绝, 清空用户信息的身份认证信息
                userInfo.setRealAuthId(null);
            }
            userInfoService.update(userInfo);

        }

    }
}
